<?php
/*
  功能：角色权限关联 model
  作者：cxm
  时间：2018-09-21
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class AdminRolePermission extends BaseModel {
//    use SoftDeletes;
//    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='admin_role_permission';  //绑定表

    /**
     * 获取权限分配菜单ID
     * @param $user_id int 用户ID
     * @return array
     */
    public static function getAuthorityMenuId($user_id)
    {
        $role_id = AdminUserRole::byUserIdGetRoleId($user_id);
        $menu_ids = self::where('role_id', $role_id)->get(['permission_id'])->toArray();
        $data = [];
        if ($menu_ids) {
            foreach ($menu_ids as $item) {
                $data[] = $item['permission_id'];
            }
        }
        return $data;
    }

    /**
     * 获取指定角色的权限
     * sql 运行时间 0.048s
     * @param $role_id 角色ID
     * @return mixed
     */
    public static function gePermissionId($role_id){
        // 查询权限内的路由
        $menus = self::where('role_id',$role_id)->pluck('permission_id')->toArray();
        return $menus;
    }

    /**
     * 当前角色的权限
     * @param $role_id 角色ID
     * @return mixed
     */
    public static function getPermissionByRoleId($role_id){
        // 用户的权限ID
        $menu_ids = self::where('role_id', $role_id)->get(['permission_id'])->toArray();
        $data = array_column($menu_ids,'permission_id');
        return $data;
    }
}